package com.wswy.wzcx.widget;

import android.content.Context;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.ScrollView;


public class JudgeNestedScrollView extends ScrollView {
    private boolean isNeedScroll = true;
    private float xDistance, yDistance, xLast, yLast;
    private int scaledTouchSlop;

    private GestureDetector mGestureDetector;

    public JudgeNestedScrollView(Context context) {
        super(context,null);
    }

    public JudgeNestedScrollView(Context context, AttributeSet attrs) {
        super(context, attrs,0);
    }

    public JudgeNestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mGestureDetector = new GestureDetector(context, new YScrollDetector());

        scaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                xLast = ev.getX();
                yLast = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();

                xDistance += Math.abs(curX - xLast);
                yDistance += Math.abs(curY - yLast);
                xLast = curX;
                yLast = curY;
                //Log.e("SiberiaDante", "xDistance ：" + xDistance + "---yDistance:" + yDistance);

                if (xDistance > yDistance ){
                    return false;
                }

//                if (isNeedScroll){
//                    return super.onInterceptTouchEvent(ev);
//                }
//
//                //return !(xDistance > yDistance || yDistance < scaledTouchSlop) && isNeedScroll;
//                if (isNeedScroll){
//                    getParent().requestDisallowInterceptTouchEvent(true);
//                    return super.onInterceptTouchEvent(ev);
//                }else {
//                    getParent().requestDisallowInterceptTouchEvent(false);
//                }
//
//                return false;
        }
        return super.onInterceptTouchEvent(ev);
    }


//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
//    }

    public void setNeedScroll(boolean isNeedScroll) {

        if (this.isNeedScroll != isNeedScroll){

        }
        this.isNeedScroll = isNeedScroll;
    }

    private static class YScrollDetector extends SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return Math.abs(distanceY) > Math.abs(distanceX);
        }
    }
}